package com.adinnet.api.sign.controller;


import com.adinnet.api.child.model.Child;
import com.adinnet.api.child.service.ChildService;
import com.adinnet.api.sign.model.Sign;
import com.adinnet.api.sign.model.VO.SignReq;
import com.adinnet.api.sign.service.SignService;
import com.adinnet.core.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 签到记录 前端控制器
 * </p>
 *
 * @author chenjia
 * @since 2020-12-03
 */
@RestController
@RequestMapping("api/sign")
public class SignController {

    @Autowired
    private SignService signService;

    @Autowired
    private ChildService childService;

    /**
     * 查询某月签到记录
     * @param year 年份参数
     * @param month 月份参数
     * @param childId 宝宝id
     * @return
     */
    @GetMapping("getSignRecord")
    public JsonResult getSignRecord(@RequestParam(value = "year") Integer year,@RequestParam(value = "month") Integer month, @RequestParam(value = "childId") Integer childId) {
        try {
            HashMap<Object, Object> data = new HashMap<>();
            Child child = childService.getById(childId);
            if(child==null){
                return JsonResult.error("未查到儿童信息");
            }
            List<Sign> signs=signService.getSignRecord(year,month-1,childId);
            Integer isSign=signService.getIsSign(childId);
            Integer days=signService.getContinueSignNum(child);
            data.put("days",days);
            data.put("signRecord",signs);
            data.put("isSign",isSign);
            return JsonResult.ok().put("data",data);
        }catch (Exception e){
            return JsonResult.error("操作失败");
        }
    }


    /**
     * 签到
     * @param signReq
     * @return
     */
    @PostMapping("sign")
    public JsonResult sign(@RequestBody SignReq signReq) {
        try {
            if (signReq.getChildId()==null){
                return JsonResult.error("儿童id不能为空");
            }
            return signService.sign(signReq.getChildId());
        }catch (Exception e){
            return JsonResult.error("操作失败");
        }
    }
}
